import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        VStack {
            // 在 VStack 中添加其他视图或者组件
            Text("Hello, World!")
            
            // 添加一个 WebView 组件
            WebView(urlString: "http://localhost:5500/input.html")
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            
            WebView(urlString: "http://localhost:5500/input.html")
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        }
    }
}

struct WebView: UIViewRepresentable {
    let urlString: String

    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        var view = WKWebView();
        return view;
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {
        if let url = URL(string: urlString) {
            let request = URLRequest(url: url)
            uiView.load(request)
        }
    }
    
    // 当 WKWebView 加载完成后，执行 JavaScript 来触发焦点
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.getElementById('aa').value = 'init';", completionHandler: nil)
    }
}


#Preview {
    ContentView()
}
